package cn.panda.edu.student;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import cn.panda.edu.service.ThesisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;

import cn.panda.edu.model.Student;
import cn.panda.edu.model.Thesis;
import cn.panda.edu.util.Constant;
import cn.panda.edu.util.FileUtil;
import cn.panda.edu.util.ResultDataUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/api/student/thesis")
public class StudentThesisController {
	@Autowired
	private ThesisService thesisService;
	@Autowired
	private MessageSource messageSource;
	
	@ApiOperation(value = "获取论文列表", notes = "获取所有的论文列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageIndex", value = "当前页", required = true, dataType = "Integer", paramType = "form") })
	@RequestMapping(value = "/showThesisByPage", method = RequestMethod.POST)
	@ResponseBody
	public Object showThesisByPage(@RequestParam(defaultValue = "1", name = "pageIndex") Integer pageIndex, HttpSession session) {
		Student student = (Student) session.getAttribute(Constant.LOGIN_USER);
		PageInfo<Thesis> pageInfo = thesisService.listAllByStuId(student.getStudentId(), pageIndex, Constant.PAGE_SIZE);
		return ResultDataUtil.resData(200, "", pageInfo);
	}

	@ApiOperation(value = "论文下载", notes = "论文下载")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "文件id", required = true, dataType = "Integer", paramType = "form") })
	@RequestMapping(value = "/download", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> downloadFile(HttpServletResponse res,HttpServletRequest request,Integer id) {
		 Thesis thesis = thesisService.selectByPrimaryKey(id);
		 String fileName = thesis.getThesisTitle();
		 int status = 200;
		 String msg = null ;
		 String filePath = null ;
		 try {
			filePath = thesis.getThesisAddress();
			FileUtil.downloadFile(filePath, res);
			msg= "下载成功" ;
		} catch (Exception e1) {
			status = 500;
			msg= "下载失败" ;
			e1.printStackTrace();
		} // 各浏览器基本都支持ISO编码
		
		return ResultDataUtil.resData(status, msg, filePath);
	}


	@ApiOperation(value = "提交论文", notes = "学生提交论文")
	@ApiImplicitParams({ @ApiImplicitParam(name = "file", value = "上传的文件", required = false, dataType = "file") })
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addThesis(@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request, Thesis thesis, HttpSession session)throws Exception {
		String path = FileUtil.uploadFile(file);
		Student student = (Student) session.getAttribute(Constant.LOGIN_USER) ;
		thesis.setStudentId(student.getStudentId());
		thesis.setThesisTitle(file.getOriginalFilename().substring(0, file.getOriginalFilename().indexOf(".")));
		thesis.setThesisAddress(path);
		thesis.setThesisDate(new Date());
		int i = thesisService.insertSelective(thesis);
		if (i > 0) {
			return ResultDataUtil.resData(200, "提交成功", null) ;
		}
		return ResultDataUtil.resData(500, "提交失败", null) ;
	}

}
